import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    role: [],//设置权限
    detailData: [],//商品详情数据
    orderId: 0,//订单ID
    changedOrderId: 0,//改变订单ID
    changedKnowledgeId: 0,
    user: '',//用户
    loginUserNickname: '',//登录用户的昵称
    loginUserAvatar: '',//登录用户头像
    activeIndex: '1',//激活的索引
    publishActiveIndex: '1',//发布激活的索引
    userActiveIndex: '1-1',//用户模块的索引
    // 从localStorage获取token值
    token: localStorage.getItem('token') 
    ? localStorage.getItem('token') : '',
    paymentInfo: '',//付款信息
    // 图片开发环境展示路径
    imgShowRoad: process.env.VUE_APP_Address,
    // 目录上传开发环境路径
    fileUploadRoad: process.env.VUE_APP_Address,
    // 多文件
    mutiFile:'',
  },
  getters: {
    // 设置token
    getStorage(state) {
      if (!state.token) {
        state.token = JSON.parse(localStorage.getItem(key))
        return state.token
      }
    },
    // 判断权限是否为专家
    isExpert(state) {
      for (var k in state.role) {
        if (state.role[k].authority === "expert") {
          return true;
        }
      }
      return false;
    },
    // 判断权限是否为admin
    isAdmin(state) {
      for (var k in state.role) {
        if (state.role[k].authority === "admin") {
          return true;
        }
      }
      return false;
    },
  },
  // 修改状态
  mutations: {
    // 更新订单ID
    updateOrderId(state, value) {
      state.orderId = value
    },
    // 更新详情页数据
    updateDetailData(state, value) {
      state.detailData = value
      console.log('state.detailData',state.detailData)
    },
    // 更新改变订单ID
    updateChangedOrderId(state, value) {
      state.changedOrderId = value
    },
    // 更新改变知识ID
    updateChangedKnowledgeId(state, value) {
      state.changedKnowledgeId = value
    },
    // 更新登录用户的昵称
    updateLoginUserNickname(state, value) {
      console.log('state.loginUserNickname',value,state.loginUserNickname)
      state.loginUserNickname = value
    },
    // 更新登录用户的头像
    updateLoginUserAvatar(state, value) {
      state.loginUserAvatar = value
    },
    // 更新登录用户权限
    updateRole(state, value) {
      state.role = value
    },
    // 更新支付信息
    updatePaymentInfo(state, value) {
      state.paymentInfo = value
    },
    // 更新激活索引
    updateActiveIndex(state, value) {
      state.activeIndex = value
    },
    // 更新发布激活索引
    updatePublishActiveIndex(state, value) {
      state.publishActiveIndex = value
    },
    // 更新用户激活索引
    updateUserActiveIndex(state, value) {
      state.userActiveIndex = value
    },
    // 设置存储token
    setToken(state, value) {
      state.token = value;
      localStorage.setItem('token', value)
    },
    // 删除token
    removeStorage(state) {
      localStorage.removeItem('token');
    }
  },
  actions: {
  },
  modules: {
  }
})
